package sns.action;

import org.springframework.transaction.annotation.Transactional;

import sns.bean.User;
import sns.service.UserService;
import sns.websocketservlet.NHWCWebSocketServlet;

import com.opensymphony.xwork2.ActionSupport;

public class GotoNHWCAction extends ActionSupport {

	private static final long serialVersionUID = 1L;

	// dao and service
	private UserService userService;
	// input value
	private Integer roomid;
	// output value
	private Integer gameid;
	private Integer iduser;
	private String nickname;
	private String errormsg;

	public String getErrormsg() {
		return errormsg;
	}

	@Transactional
	public String execute() {
		User curuser = userService.getCurrentUser();
		iduser = curuser.getIduser().intValue();
		nickname = curuser.getNickname();
		if (NHWCWebSocketServlet.alreadyInGame(iduser)) {
			errormsg = "你有正在进行的游戏";
			return "alreadyInGame";
		}
		if (roomid == null) {
			roomid = -1;
		}
		gameid = NHWCWebSocketServlet.getGameId(roomid);
		if (gameid == -1) {
			errormsg = "没有空余的游戏房间";
			return "noAvailableGame";
		} else if (gameid == -2) {
			errormsg = "该房间已满或正在进行游戏";
			return "gameNotAvailable";
		}

		return ActionSupport.SUCCESS;
	}

	public Integer getGameid() {
		return gameid;
	}

	public void setRoomid(Integer roomid) {
		this.roomid = roomid;
	}

	public Integer getIduser() {
		return iduser;
	}

	public String getNickname() {
		return nickname;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

}
